<?php

Yii::import('application.extensions.PHPMailer.EcsSendMail');

class CollegeDetailsController extends GxController {

    public $layout = 'applicationpage';

    public function actionPassword() {
        if (!empty(Yii::app()->session['collge_id'])) {
            $model = $this->loadModel(Yii::app()->session['collge_id'], 'CollegeDetails');
            $this->render('_collegePassword', array(
                'model' => $model,
            ));
        }
    }
   public function actionUpdateCollgeInfor() {
        if (isset($_POST['CollegeDetails']['hod_first_name']) AND ! empty($_POST['CollegeDetails']['hod_first_name'])) {
            $password = substr($_POST['CollegeDetails']['hod_first_name'], 0, 2) . substr($_POST['CollegeDetails']['hod_email'], 3, 5) . substr($_POST['CollegeDetails']['hod_mobile_number'], 4, 7);
            College::model()->updateByPk(Yii::app()->session['collge_id'], array("hod_title" => $_POST['CollegeDetails']['hod_title'],
                "hod_first_name" => $_POST['CollegeDetails']['hod_first_name'],
                "hod_last_name" => $_POST['CollegeDetails']['hod_last_name'],
                "hod_email" => $_POST['CollegeDetails']['hod_email'], "hod_mobile_number" => $_POST['CollegeDetails']['hod_mobile_number'], 'password' => $password));
            $adminSubject = 'Password for Update College information';
            $adminTpl = 'sendCollegePassword.php';
            $this->sendEmail($_POST['CollegeDetails']['hod_first_name'], $password, $_POST['CollegeDetails']['hod_email'], $adminSubject, $adminTpl);
            echo "Please check your mail.your password has been send on mail and your mobile no. ";
        }
    }
    public function actionregisterstep3() {
        $model = new CollegeDetails;
        if (isset($_POST['CollegeDetails'])) {
            $model->setAttributes($_POST['CollegeDetails']);

            if ($model->save()) {
                if (Yii::app()->getRequest()->getIsAjaxRequest())
                    Yii::app()->end();
                else
                    $this->redirect(array('view', 'id' => $model->id));
            }
        }

        $this->render('_form', array('model' => $model));
    }

    public function actionCollgeInfoUpdate() {
        if (!empty(Yii::app()->session['collge_id'])) {
            $model = $this->loadModel(Yii::app()->session['collge_id'], 'CollegeDetails');
            if (isset($_POST['CollegeDetails'])) {
                $model->setAttributes($_POST['CollegeDetails'],FALSE);
                if ($model->validate()) {
                    $model->save(false);
                    Yii::app()->session['collge_id'] = '';
                    $this->render('_thankyou');
                    die;
                }
            }

            $this->render('update', array(
                'model' => $model,
            ));
        } else {
            throw new CHttpException(404, 'The specified request cannot be found.');
        }
    }

    public function actionCheckPassword() {
        if (!empty($_POST['password'])) {
            $password = CollegeDetails::model()->findByAttributes(array('id' => Yii::app()->session['collge_id'], 'password' => $_POST['password']));
            if (count($password) > 0) {
                echo 1;
                die;
            } else {
                echo 'Please enter currect password.';
            }
        }
    }

    public function sendEmail($userName, $password, $to, $subject, $tpl) {
        $emalDir = ECSconfig::getEmailDir();
        $mail['to'] = $to;
        $mail['subject'] = $subject;
        $mail['msgtpl'] = $emalDir . $tpl;
        $mailReplaceArr = array('$USER_NAME' => $userName, '$PASSWORD' => $password);

        EcsSendMail::SendMail($mail, $mailReplaceArr);
    }

    public function actionCollgeInfo() {
        $model = new CollegeDetails;
        $pass = '';
        if (isset($_POST['CollegeDetails'])) {
            $model->attributes = $_POST['CollegeDetails'];
            $pass = 1;
            $password = rand(substr($_POST['CollegeDetails']['phone_number'], 4, 7), 3);
            $model->setAttribute('password', $password);
            if ($model->validate()) {
                $model->save(false);
                Yii::app()->user->setFlash('Password', 'Please check your mail and mobile no.i have send a password.');
                Yii::app()->session['collge_id'] = $model->id;
                $adminSubject = 'Password for Update College information';
                $adminTpl = 'sendCollegePassword.php';
                $this->sendEmail($_POST['CollegeDetails']['name_of_the_head_institution'], $password, $_POST['CollegeDetails']['college_email'], $adminSubject, $adminTpl);
                $this->render('_password_text');
                die;
            }
        }

        $this->render('_createCollege', array(
            'model' => $model, 'pass' => $pass
        ));
    }
    public function actioncollgeAddress() {
        $select = '';
        $data = College::model()->findAllByAttributes(array('id' => (int) $_POST ['College']['id']));
        $data = CHtml::listData($data, 'id', 'college_address');
        if (empty($data)) {
            $select = CHtml::tag('option', array('value' => ''), 'Select collge address', true);
        } else {
            foreach ($data as $value => $name) {
                $select.= CHtml::tag('option', array('value' => $value), CHtml::encode($name), true);
            }
        }
        echo $select;
        die;
    }
    public function actionfindCollege() {
        $select = '';
        $data = CollegeDetails::model()->findAllByAttributes(array('states_id' => (int) $_POST ['State']['id']));
        $data = CHtml::listData($data, 'id', 'name_of_institution');
        if (empty($data)) {
            $select = CHtml::tag('option', array('value' => ''), 'Select collge name', true);
        }
        foreach ($data as $value => $name) {
            $select.= CHtml::tag('option', array('value' => $value), CHtml::encode($name), true);
        }
        echo $select;
        die;
    }

    public function actionCollgeInfoDetails() {
        if (!empty(Yii::app()->session['collge_id'])) {
            $model = $this->loadModel(Yii::app()->session['collge_id'], 'CollegeDetails');
            $this->render('_view', array(
                'model' => $model,
            ));
        } else {
            throw new CHttpException(404, 'The specified request cannot be found.');
        }
    }

    public function actionIndex() {
        $model = new CollegeDetails;
        if (isset($_POST['State']['id'])) {
            $college = CollegeDetails::model()->findByAttributes(array('states_id' => $_POST['State']['id'], 'id' => $_POST['College']['id']));
            if (!empty($college)) {
                Yii::app()->session['collge_id'] = $college->id;
                $this->redirect(array('/collegeDetails/collgeInfoDetails'));
            } else {
                Yii::app()->user->setFlash('noresult', 'No record found.please click on i can not found link and create a collge.');
            }
        }
        $this->render('_college_search', array(
            'model' => $model,
        ));
    }

}
